package util

import (
	"fmt"
	"regexp"
	"repairs/pkg/gredis"
	"strconv"

	"repairs/pkg/setting"
)

// Setup Initialize the util
func Setup() {
	jwtSecret = []byte(setting.AppSetting.JwtSecret)
	hmacSecret = []byte(setting.AppSetting.HmacSecret)
}

func ConvertPosition(position [2]float64) string {
	return strconv.FormatFloat(position[0], 'f', 6, 32) +
		"," +
		strconv.FormatFloat(position[1], 'f', 6, 32)
}

// CheckAccountNumber 正则匹配合法账号
func CheckAccountNumber(account string) bool {
	// 现支持学号
	var re = regexp.MustCompile(`(?m)20[12][0-9]250[0-9]{4}`)
	return re.MatchString(account)
}

// GenerateRentKey 生成用户租借的凭证
// 今日访问数_用户名
func GenerateRentKey(username string) string {
	const visitKey = "visit_count"
	bytes, err := gredis.Get(visitKey)
	if err != nil {
		return ""
	}
	_ = gredis.Incr(visitKey)
	return fmt.Sprintf("%s_%s", string(bytes), username)
}
